import router from '@/router'
// 导入进度条组件及样式
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import store from './store'

// 前路由守卫
const whiteList = ['/login', '/404']// 白名单列表,不用登录就可以去的地方
router.beforeEach((to, from, next) => {
  // console.log('前置守卫', to, from)
  NProgress.start()
  // 判断是否有token值
  if (store.state.user.token) {
    // 如果已经登录过,想要去登录页,就强行跳转
    // if (to.path === '/login') {
    if (whiteList.includes(to.path)) {
      next('/')
      NProgress.done()
    } else {
      store.dispatch('user/getUserInfoAction')
      next()
    }
  } else {
    if (to.path === '/login') {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  // console.log('后置守卫')
  NProgress.done()
})
